<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Banner_model extends CI_Model {

	var $title   = '';
	var $content = '';
	var $date    = '';

	function __construct()
	{ 
		parent::__construct();
	}
 
	function get_all_banner($num,$offset)
	{
		$this->load->database();
		$query = $this->db->get('tbl_banner',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		} else {
			return array();
		} 
	}
	
	public function get_banner_details($cateid){
		$this->load->database();
		$this->db->where('cateid',$cateid);
		$query = $this->db->get('tbl_banner');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		} else {
			return array();
		} 
	}

	function total_banner(){
		$this->load->database();
		return $this->db->count_all('tbl_banner'); 
	}
	
	function delete_banner($id)
	{
		$this->load->database();
		$this->db->delete('tbl_banner', array('id' => $id));
	}

	//return 1: have banner
	//return 0: dont have banner
	function checkexit_banner($cateid){
		$this->load->database();
		$this->db->where('cateid',$cateid);
		$query = $this->db->get('tbl_banner');
		if ($query->num_rows() > 0)
		{
			return 1;
		} else {
			return 0;
		} 
	}
	
	// check exit banner first
	// return 1: save successfully
	// return 0: save failed
	function insert_banner($cateid,$imageurl)
	{
		$result = $this->checkexit_banner($cateid);
		if($result == 0){
			$this->load->database();
			$data = array(
				'cateid' => $cateid ,                
				'image_url' =>  $imageurl
			);
			$this->db->insert('tbl_banner', $data);
			return 1;
		}else{
			return 0;
		}
	}
	
	//update status to active
	function update_banner_status($id,$status){
		$this->load->database();
		$data = array(
				'status'=>$status,  
		);
		$this->db->where('id', $id);
		$this->db->update('tbl_banner', $data);
	}

	 

}